package cn.kasa.controller;

import cn.kasa.User;
import cn.kasa.handler.UserBlockHandler;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope // 刷新配置
public class UserController {

    @Value("${test}")
    private Long test;


    @GetMapping("/getUserById/{id}")
    @SentinelResource(value = "getUserById",
            blockHandler = "getUserByIdBlockHandler",
            blockHandlerClass = UserBlockHandler.class,
            fallback = "getUserFallback"
            )
    public User getUserById(@PathVariable Long id){
        int i = 1/0;
        return User.builder().id(test).name("zs").intro("哈哈哈").build();
    }

     public static User getUserFallback(@PathVariable Long id, Throwable t){
         return User.builder().id(-2L).name("fallback").intro("fallback").build();
    }

}
